package com.pdwbdn.utils;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;

import java.security.interfaces.RSAPrivateKey;

public class DNFUtil {

    /**
     -----BEGIN PUBLIC KEY-----
     MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAh0GclScWgHE6nyW6AnuaYCm1Cv0MquH4
     DBran9pJTiyr+F38+cj+bSw/bp398gNXDU5RaLpq/k7Lr/f6K0vLY5BKRjzG4gms5mvnhArwQiPg
     ZsbXGeGfBD8Y+jvfEWy2TpYN49suVm8GVtO/LzAua3y5THzpntWqxg6o1gOhCXCvQf/zsaUuNPbf
     VaKULrVeP8A3IbI9BNfibuUMGOdyaZvGnbuq4jdMLF4I1T2eQuvt5TQk7zTbJwMuua/jxBGYsjPK
     lor9NAI+r9RA7IpXf1Hp89xI+FZD6AoaGnAezJVYsgodgleE3W9VDOpJsa1hh0shw6D91wiiqqrK
     6ttTKVtfDvqYMGMVDSn21ANFrZn9HuvVbMxPU5gTOvENdDSARFZvjspKdVYp/GkKAY21xV5dIRe6
     jWQ7osGCvOMhcbzxf8u1Eu7qOTfUJIgHicmcxKr6hbiPZw43FGXquT3LOIKfFK4IEw+rsYXCG3tW
     7Xkc4b2Thxwk6YvDl/vfH8SO9jlx1Tw3QDY8Je/Ll6fWRjMttxdXmhtXMrYFwpqv6g1Ukdkim5TP
     7BUUjs3WInqm0uPOm44bxuY6WjkiLk/CqC+iFxsc47PMzUrsciZh/jiuSKRDT/lDB18LFvLBxdBN
     uh3eUOiMhlb/2VLwd018Na5adf0O+iW1V45xXNNLsyUCAwEAAQ==
     -----END PUBLIC KEY-----
     */

    private final static String privateKeyStr = "MIIJQgIBADANBgkqhkiG9w0BAQEFAASCCSwwggkoAgEAAoICAQCHQZyVJxaAcTqfJboCe5pgKbUK" +
            "/Qyq4fgMGtqf2klOLKv4Xfz5yP5tLD9unf3yA1cNTlFoumr+Tsuv9/orS8tjkEpGPMbiCazma+eE" +
            "CvBCI+BmxtcZ4Z8EPxj6O98RbLZOlg3j2y5WbwZW078vMC5rfLlMfOme1arGDqjWA6EJcK9B//Ox" +
            "pS409t9VopQutV4/wDchsj0E1+Ju5QwY53Jpm8adu6riN0wsXgjVPZ5C6+3lNCTvNNsnAy65r+PE" +
            "EZiyM8qWiv00Aj6v1EDsild/Uenz3Ej4VkPoChoacB7MlViyCh2CV4Tdb1UM6kmxrWGHSyHDoP3X" +
            "CKKqqsrq21MpW18O+pgwYxUNKfbUA0Wtmf0e69VszE9TmBM68Q10NIBEVm+Oykp1Vin8aQoBjbXF" +
            "Xl0hF7qNZDuiwYK84yFxvPF/y7US7uo5N9QkiAeJyZzEqvqFuI9nDjcUZeq5Pcs4gp8UrggTD6ux" +
            "hcIbe1bteRzhvZOHHCTpi8OX+98fxI72OXHVPDdANjwl78uXp9ZGMy23F1eaG1cytgXCmq/qDVSR" +
            "2SKblM/sFRSOzdYieqbS486bjhvG5jpaOSIuT8KoL6IXGxzjs8zNSuxyJmH+OK5IpENP+UMHXwsW" +
            "8sHF0E26Hd5Q6IyGVv/ZUvB3TXw1rlp1/Q76JbVXjnFc00uzJQIDAQABAoICADh8lFJOMr2NjDfu" +
            "35It4LkMcgmWyPWMzbTweiGMXlUBnwho6D/BS1B4zDz8SpQKBQ8PiDw+rnRqn/Jrk9UFIHoCRqKy" +
            "uzSnacgJhHTGxLQ6m3sQy8CJU4nIeDv5SR3T0wda744beWyrjXAxmoSwkJJfUlu5RzAId/ujH6vR" +
            "dC2nG+pqZHHqVDPWbksrTMt8/1GMivnNoGInbu91ROoZrtSgNACn4aP8CBn3Y6T6yN+V/JJehpZH" +
            "8saGM7tk07CM0MKLkq3wefKAhYGVGVAwLn6DzTsE4oykKlP79A7aiGJUdOhWBSDsQBbiahist/42" +
            "NKb2H7G9yIVHm/e+wGP5GO9tq0Gh3HANhZU0Agi0RwxXmtzgQmsdW/JQpgyDMOYDUmVVTrzveQpG" +
            "PPxEYUc3WTvMDmAKuqoSc1UOoq/4zGu9w1cRMvRUdJC7iDdxaTW5+rNr/M3LQqIibYYGPbzZlVNP" +
            "/uhN44hORtqwXG7p4ebNH3LoPefXE9rYCcu7iIqkjBZuSUKQ2CxHhIqufubpvNKLdfKamTwsGpEt" +
            "Xm1anSJ9YM8iS8GiDaaieNEEp2n8wCaf3mpjZG90EnNkRfOkLpoB0Y5DyMYWFBaRfPOKR1BzjiZI" +
            "ZhvJqo8syBHL8T6YkhQLQChAmusTpFcODNOMZUg8r7aVVYxOG3ztpO8ThZd5AoIBAQDW0l8ntmDR" +
            "sZTIoJO39z+JFb4WSVxkhdAprSnZSxudkpZp7Y5i7qBrFyCe1SiLb5DSsetlfEywOpnBU+QfhgNO" +
            "Rmb9Nxa94R+kKQq8qEolSLLhes8puRCFZhmQQacYDfsEBcMjWBa9CjJiDJBBpJkUZ/VkLZL4cPdN" +
            "5NqD1YnJtIPBIcn3WX5oChgAZC/sw4xTbRsUTb/YYc2b69oIltvxvm83Bc0laVTQjiJF/olhtLls" +
            "JYfGVJ4j5OTYOvhf5Fc4ESUQe29UMwwV4XU/5VNQk5ioVno0AlFVt734qp4B5jtze96rDZH0bWPs" +
            "UV5Nj+AKfw+FxSuZxjRIrjHKGEcbAoIBAQChLtee+HcfmYHBjVqq5kQGsovZUFIuETzTsQz8BdQI" +
            "Ti5BBgyeAD8ajtdcwh+x//0WTUhuEdXz6VHc/4z9CGRw9Yqc02wxjQuVV9QY2Uen6oN/h+PTzfaP" +
            "QS2IH63nsA/4DxLnkTrquTUkuj75ab3aCELLnPzdwwF9EuCpjSENqlMYcbU8f1pcW6y+nvgolYnt" +
            "JEcUKBNETGzA6WoHiI4OYtsNKYJDe/wdJx5XiaQ+u1UV0K1AUNAyf+PfKQV3hslIuDW6bPvWuQHm" +
            "DKSmVYIBgJCuy0rWryDbe5SAUcIebMOpbbsh/HqeU5RDXFGc+eQSkDATd/Wb97bk/o4LuFK/AoIB" +
            "AEf/mA7KTGbber9HOlslDfFXDS+RBNtYEe118Rm315iXQbxc9yTnT3K0/KTfUkhxavuC0B9iap2B" +
            "9nhaMpHyK3d3lxr09LVNF3rAmq2iRb97PyDdNUOJmCTtlFHJ/0PLo760Y7Ob2XsgSMSd61dk4D17" +
            "nj4mHCUST4CW3DnlPtoQmzwGpBc3smsSPvsamQlkfMqVPccvS8Ys+WlxwdCxJyQ9rC6nzhrk051W" +
            "ldwZtFe/54l8hqYNRDdJhv2Fvir9b9IFxQNiK2zv8vP+bXqt1J+j6D07RpfZ0wLEo3INVQnT7UcG" +
            "VBnGYY2ngB77In0iZHeAIGa4L6XJTUqyv1M5kdMCggEBAJwd18DlHdRghbpn5FrFXzwxcZ0R3Bc8" +
            "oPLVeSMdrgpBVzGp30ymJnjdw6DXcYWh0YvpXO8nnVh7M8xfRTrMByUV3qUZbqDr4T1MbWWIBoyQ" +
            "a795amT7CAYm9ifzryhKcp0sVH4wP6Wbr+qb7mT7U2S8PZ4jtxRATc/MGgh/ZnVq9MMnV4h6PZZZ" +
            "QQ78wJXmiUpEqgfO7SJo96k+K3GShS1P+KVeAArHKyE9g3j8GzabkcTocvZpj85p6td6Tw4/YfRq" +
            "X7fq2V5XoYwl0NA+4AX+daSnyRv5UlTj9Kj5B5EafcU8QFWhy05Q27kG0RG/RHXqbHZe6Wig3Ogd" +
            "2o1lgdcCggEAc4rVLm8HV/nx6DAEb5BbPRVqfsO505AhCAMex/gVnFCNqPu3w+znZ3Ib+6gTbjWp" +
            "kWzC76w4X+agjDzKSOOCbUCbMtGCMS/Ttal8VPLMjob8C6pHEg/+UwAD5LzSUg8CVJh7C86Z8yiw" +
            "VfD/ZAaxDJ7GRA7hiG49MI+ecDcBSO2iHE3k3kMirwJ7u6wVDtDA/htay1iKndieiGdLzmZFKr1x" +
            "mocRY9PSG/sSS12o4E+OBmKUw+tWwr/hjJ+hiYisYj5k/+CyfxCGNODhSqkERvC56woEH1eXWMQd" +
            "9lkXyvxfNgdlOoGZNGr1ftips/uFCv979hD5iIA3AHoq0VvKcA==";

    public static String createToken(long uid) {
        try {
            String hex = String.format("%08x" +
                    "0101010101010101010101010101010101010101010101010101010101010101" +
                    "55914510010403030101", uid);
            byte[] data = Hex.decodeHex(hex);
            RSAPrivateKey privateKey = RSAUtil.loadPrivateKeyByStr(privateKeyStr);
            if (privateKey == null) {
                return null;
            }
            byte[] tokenBytes = RSAUtil.encrypt(privateKey, data);
            if (tokenBytes == null) {
                return null;
            }
            return Base64.encodeBase64String(tokenBytes);
        } catch (Throwable t) {
            System.err.println(t.toString());
            return null;
        }
    }
}

